///////////////////////////////////////////////////////////////////////////////
// \author (c) GuJianGongRen
//             2024, Shanghai, China
//
// \license The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// \brief usr_task.c
//
// \version
// v0.0.1: 2024.04.06, Initial version.
///////////////////////////////////////////////////////////////////////////////

/* Includes ------------------------------------------------------------------*/
#include "usr_task.h"
#include "stm32f429i_discovery.h"
#include "uart.h"

TaskHandle_t StartTask_Handler;
TaskHandle_t Led3Task_Handler;
TaskHandle_t Led4Task_Handler;
TaskHandle_t FloatTask_Handler;

void start_task(void *pvParameters)
{
    taskENTER_CRITICAL();

    xTaskCreate((TaskFunction_t)led3_task,
                (const char*)"led3_task",
                (uint16_t)LED3_STK_SIZE,
                (void*)NULL,
                (UBaseType_t)LED3_TASK_PRIO,
                (TaskHandle_t*)&Led3Task_Handler);

    xTaskCreate((TaskFunction_t)led4_task,
                (const char*)"led4_task",
                (uint16_t)LED4_STK_SIZE,
                (void*)NULL,
                (UBaseType_t)LED4_TASK_PRIO,
                (TaskHandle_t*)&Led4Task_Handler);

    xTaskCreate((TaskFunction_t)float_task,
                (const char*)"float_task",
                (uint16_t)FLOAT_STK_SIZE,
                (void*)NULL,
                (UBaseType_t)FLOAT_TASK_PRIO,
                (TaskHandle_t*)&FloatTask_Handler);

    vTaskDelete(StartTask_Handler);

    taskEXIT_CRITICAL();
}

void led3_task(void *pvParameters)
{
    while(1)
    {
        BSP_LED_Toggle(LED3);
        vTaskDelay(500);
    }
}

void led4_task(void *pvParameters)
{
    while(1)
    {
        BSP_LED_Off(LED4);
        vTaskDelay(200);
        BSP_LED_On(LED4);
        vTaskDelay(800);
    }
}

void float_task(void *pvParameters)
{
    static float float_num = 0.00;
    static int64_t ll0 = 0x8000000000000000LL;
    static uint64_t ll1 = 0xFFFFFFFFFFFFFFFFLL;
    static int64_t ll2 = 0x7FFFFFFFFFFFFFFFLL;
    static uint64_t ll3 = 0x0FFFFFFFFFFFFFFFLL;
    while(1)
    {
        float_num += 0.01f;
        printf("float_num = %.4f\n", float_num);
        printf("ll0 = %lld, ll1 = %llu, ll2 = %lld, ll3 = %llu\n", ll0++, ll1--, ll2--, ll3++);
        vTaskDelay(1000);
    }
}
